const http = require('http')
const { createProxyMiddleware } = require('http-proxy-middleware')

const server = http.createServer((req, res) => {
  let urlString = req.url
  // 将ajax路径下的请求都代理到http://lady.vip.com中
  if (/\/ajax/.test(urlString)) {
    const proxy = createProxyMiddleware('/ajax', {
      target: 'http://lady.vip.com',  // 代理域名
      changeOrigin: true,  // 改变源
      pathRewrite: {
        '^/ajax': ''
      }  // 重写路径,上面是将/ajax这个路径给替换成空字符串 '' 了，也就相当于截去了/ajax这段路径
    })

    proxy(req, res)
  } else {
    console.log('error')
  }
})

server.listen(5000, () => {
  console.log('5000端口服务器已启动');
})